from django.http import HttpResponse,JsonResponse
from django.shortcuts import render, redirect
from ocr.run import main

def upload(request):
    result = {}
    for item in request.FILES:
        obj = request.FILES.get(item)
        '''写入图像到本地项目主路径下'''
        # with open(obj.name, 'wb') as f:
        #     f.write(obj.read())
        result['type'], result['res'] = main(obj.read())
        # result['res'] = main(obj.read())
        # return JsonResponse(result)
    return render(request, 'index.html', result)
    # return JsonResponse(result)


def index(request):
    result = {}
    for item in request.FILES:
        obj = request.FILES.get(item)
        # print('obj:', obj.read())
        # print('obj name:', obj.name)
        '''写入图像到本地项目主路径下'''
        # with open(obj.name, 'wb') as f:
        #     f.write(obj.read())
        # context['type'], context['hello'] = main(obj.read())
        result['type'], result['res'] = main(obj.read())
        return JsonResponse(result)